﻿using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace OurSite.Business.Helper
{
    public static class ImageUtility
    {
        public static string GetImageCaption(string imagePath)
        {
            if (File.Exists(imagePath))
            {
                //parse file name
                string fileName = imagePath.Substring(imagePath.LastIndexOf("\\") + 1);
                //parse file path
                string filePath = imagePath.Substring(0,imagePath.LastIndexOf("\\"));
                //create shell instance
                Shell32.Shell shell  = new Shell32.ShellClass();
                //set the namespace to file path
                Shell32.Folder folder = shell.NameSpace(filePath);
                //get ahandle to the file
                Shell32.FolderItem folderItem = folder.ParseName(fileName);
                //did we get a handle ?
                if (folderItem != null)
                {
                    return folder.GetDetailsOf(folderItem, 21);
                }
                //clean ip
                folderItem = null;
                folder = null;
                shell = null;
            }
            return string.Empty;
        }


        public static void Resize(string imagePath, int target)
        {
            if (File.Exists(imagePath))
            {
                using (Image imgPhoto = Image.FromFile(imagePath))
                {
                    
                    int sourceX = 0;
                    int sourceY = 0;
                    float percentage;
                    int sourceWidth = imgPhoto.Width;
                    int sourceHeight = imgPhoto.Height;

                    if (sourceWidth > sourceHeight)
                        percentage = (float) target/sourceWidth; // Landscape
                    else
                        percentage = (float) target/sourceHeight; // Protrait

                    int destX = 0;
                    int destY = 0;
                    int destWidth = (int) (sourceWidth*percentage);
                    int destHeight = (int) (sourceHeight*percentage);

                    using (Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb))
                    {
                        bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

                        using (Graphics grPhoto = Graphics.FromImage(bmPhoto))
                        {
                            grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

                            grPhoto.DrawImage(imgPhoto,
                                              new Rectangle(destX, destY, destWidth, destHeight),
                                              new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                                              GraphicsUnit.Pixel);
                        }
                        bmPhoto.Save(imagePath + ".jpg", ImageFormat.Jpeg);
                    }
                }
                // Delete the original file
                File.Delete(imagePath);
                // Rename new file to old file name
                File.Move(imagePath + ".jpg", imagePath);
            }
        }

        public static void Move(string originalPath, string newPath)
        {
            newPath = newPath.Replace("/", "\\");
            string targetPath = newPath.Substring(0, newPath.LastIndexOf("\\"));

            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }

            if (File.Exists(newPath))
                File.Delete(newPath);

            File.Move(originalPath, newPath);
        }

        public static void Copy(string sourcePath, string destinationPath)
        {
            sourcePath = sourcePath.Replace("/", "\\");
            destinationPath = destinationPath.Replace("/", "\\");

            if (File.Exists(sourcePath))
            {
                string targetPath = destinationPath.Substring(0, destinationPath.LastIndexOf("\\"));

                if (!Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }

                if (File.Exists(destinationPath))
                    File.Delete(destinationPath);

                File.Copy(sourcePath, destinationPath);
            }
        }
    }
}
